package com.emapp.lccode.activity

import android.app.DatePickerDialog
import android.app.DatePickerDialog.OnDateSetListener
import android.os.Build
import android.os.Bundle
import android.view.View
import android.widget.DatePicker
import androidx.annotation.RequiresApi
import androidx.fragment.app.Fragment
import com.emapp.lccode.R
import com.emapp.lccode.adapter.ViewPgerKtAdapter
import com.emapp.lccode.base.BaseActivity1
import com.emapp.lccode.base.EventBusConfig
import com.emapp.lccode.base.EventBusModel
import com.emapp.lccode.databinding.ActivityMyorderBinding
import com.emapp.lccode.fragment.OrderKTFragment
import com.emapp.lccode.utils.setOnclickNoRepeat
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.gyf.immersionbar.ktx.immersionBar
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.util.Calendar

/**
 */
//@RequiresApi(Build.VERSION_CODES.N)
class MyOrderKtActivity : BaseActivity1<BaseViewModel, ActivityMyorderBinding>(),
    OnDateSetListener {
    var titles: ArrayList<String> = arrayListOf("全部", "待处理", "已处理")
    var fragments: ArrayList<Fragment> = arrayListOf()
    private lateinit var dialog: DatePickerDialog
    private val adapter: ViewPgerKtAdapter by lazy { ViewPgerKtAdapter(this, fragments) }
    private var time_flag=1
    override fun initView(savedInstanceState: Bundle?) {
        immersionBar {
            titleBar(mViewBind.lvTitle.ivBar)
            statusBarDarkFont(true)
            fullScreen(true)
            statusBarColor(R.color.transparent)
            navigationBarColor(R.color.white)
        }
        mViewBind.lvTitle.tvTitle.text = "我的工单"
        mViewBind.lvTitle.tvRight.visibility = View.GONE
        EventBus.getDefault().register(this)
        fragments.add(OrderKTFragment.newInstance("0"))
        fragments.add(OrderKTFragment.newInstance("1"))
        fragments.add(OrderKTFragment.newInstance("2"))
        mViewBind.viewpager.adapter = adapter
        mViewBind.viewpager.offscreenPageLimit = 3
        mViewBind.layoutTab.tabMode = TabLayout.MODE_SCROLLABLE
        TabLayoutMediator(mViewBind.layoutTab, mViewBind.viewpager) { tab, position ->
            tab.text = titles[position]
        }.attach()
        initCalendar()
    }

    override fun createObserver() {
    }


    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

    override fun onBindViewClick() {
        setOnclickNoRepeat(
            mViewBind.lvTitle.ivLeft,
            mViewBind.lvTitle.tvRight,
            mViewBind.tvStart,
            mViewBind.tvEnd,
            mViewBind.tvSearch
        ) {
            when (it.id) {
                R.id.iv_left -> {
                    finish()
                }

                R.id.tv_start -> {
                    time_flag = 1
                    dialog.show()
                }

                R.id.tv_end -> {
                    time_flag = 2
                    dialog.show()
                }
                R.id.tv_search -> {

                }
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onEventMainThread(event: EventBusModel) {
        when (event.type) {
            EventBusConfig.FINISH -> {
            }

            else -> {}
        }
    }
    private fun initCalendar() {
        val calendar = Calendar.getInstance()
        dialog = DatePickerDialog(
            this, this, calendar[Calendar.YEAR] //年份
            , calendar[Calendar.MONTH] //月份
            , calendar[Calendar.DAY_OF_MONTH]
        ) //日子
    }
    override fun onDateSet(view: DatePicker?, year: Int, month: Int, dayOfMonth: Int) {
        //val desc = String.format("%s-%02d-%02d", year, month + 1, dayOfMonth)
        val desc = String.format("%s-%d-%d", year, month + 1, dayOfMonth)
        if (time_flag == 1) {
            mViewBind.tvStart.text=desc
            EventBus.getDefault().postSticky(EventBusModel(EventBusConfig.SET_START_DATE,desc))
        } else {
            mViewBind.tvEnd.text=desc
            EventBus.getDefault().postSticky(EventBusModel(EventBusConfig.SET_END_DATE,desc))
        }
    }

}
